package com.gouaixin.bee.beeframework2.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ab.util.AbDialogUtil;
import com.ab.util.AbToastUtil;
import com.gouaixin.bee.beeframework2.R;
import com.gouaixin.bee.beeframework2.bean.Feedback;
import com.gouaixin.bee.beeframework2.bean.USER;
import com.gouaixin.bee.beeframework2.tool.PreferencesUtils;
import com.gouaixin.bee.beeframework2.tool.mLog;

import java.util.Random;

import cn.bmob.im.BmobUserManager;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by Administrator on 2015/3/9.
 */

//设置
public class B8_shezhiActivity extends FragmentActivity {
    Context context=B8_shezhiActivity.this;
    TextView zhanghao;//当前帐号
    LinearLayout newMsgNotifyBt,shakeNotifyBt,voiceNotifyBt,exitMsgNotifyBt;

    ImageView newMsgNotifyTv,shakeNotifyTv,voiceNotifyTv,exitMsgNotifyTv;
    LinearLayout resetPassword,feedback,checkUpdate,recommend,activeRelate,about,clearCache;
    TextView huancunTv;//缓存显示
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.b8_shezhi);

        initView();

        initHead();

        initContent();

        setData();

        setListener();

    }

    private void setData() {
        zhanghao.setText("当前帐号:"+ PreferencesUtils.getString(context, "cid"));

        boolean newMsgNotify=PreferencesUtils.getBoolean(context,"newMsgNotify",true);
        boolean shakeNotify=PreferencesUtils.getBoolean(context,"shakeNotify",true);
        boolean voiceNotify=PreferencesUtils.getBoolean(context,"voiceNotify",true);
        boolean exitMsgNotify=PreferencesUtils.getBoolean(context,"exitMsgNotify",true);

        if(newMsgNotify){
            newMsgNotifyTv.setVisibility(View.VISIBLE);
        }else {
            newMsgNotifyTv.setVisibility(View.GONE);
        }

        if(shakeNotify){
            shakeNotifyTv.setVisibility(View.VISIBLE);
        }else {
            shakeNotifyTv.setVisibility(View.GONE);
        }

        if(voiceNotify){
            voiceNotifyTv.setVisibility(View.VISIBLE);
        }else {
            voiceNotifyTv.setVisibility(View.GONE);
        }

        if(exitMsgNotify){
            exitMsgNotifyTv.setVisibility(View.VISIBLE);
        }else {
            exitMsgNotifyTv.setVisibility(View.GONE);
        }

    }

    private void setListener() {
        newMsgNotifyBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean newMsgNotify=PreferencesUtils.getBoolean(context, "newMsgNotify", true);
                PreferencesUtils.putBoolean(context, "newMsgNotify", !newMsgNotify);
                setData();
            }
        });

        shakeNotifyBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean shakeNotify=PreferencesUtils.getBoolean(context,"shakeNotify",true);
                PreferencesUtils.putBoolean(context, "shakeNotify", !shakeNotify);
                setData();
            }
        });

        voiceNotifyBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean voiceNotify=PreferencesUtils.getBoolean(context,"voiceNotify",true);
                PreferencesUtils.putBoolean(context, "voiceNotify", !voiceNotify);
                setData();
            }
        });

        exitMsgNotifyBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean exitMsgNotify=PreferencesUtils.getBoolean(context,"exitMsgNotify",true);
                PreferencesUtils.putBoolean(context, "exitMsgNotify", !exitMsgNotify);
                setData();
            }
        });

        resetPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mLog.e("修改用户信息");

                View mView = LayoutInflater.from(context).inflate(R.layout.dialog_resetpassword, null);
                AbDialogUtil.showDialog(mView);

                TextView title = (TextView) mView.findViewById(R.id.title);
                final EditText oldPassword = (EditText) mView.findViewById(R.id.oldPassword);
                final EditText newPassword = (EditText) mView.findViewById(R.id.newPassword);
                final EditText newPassword2 = (EditText) mView.findViewById(R.id.newPassword2);
                TextView quxiao = (TextView) mView.findViewById(R.id.quxiao);
                TextView kaitong = (TextView) mView.findViewById(R.id.kaitong);

                quxiao.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AbDialogUtil.removeDialog(context);
                    }
                });
                kaitong.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mLog.e("修改用户信息：开始实现");
                        String oldPasswordstr = oldPassword.getText().toString();
                        final String newPasswordstr = newPassword.getText().toString();
                        String newPassword2str = newPassword2.getText().toString();
                        if (oldPasswordstr.equals("") || newPasswordstr.equals("") || newPassword2str.equals("")) {
                            AbToastUtil.showToast(context, "输入内容不能为空");
                            return;
                        }
                        if (!newPasswordstr.equals(newPassword2str)) {
                            AbToastUtil.showToast(context, "新密码不一致");
                            return;
                        }
                        USER myuser = BmobUser.getCurrentUser(context, USER.class);
                        String password=PreferencesUtils.getString(context,"password");


                        mLog.e("oldPasswordstr="+oldPasswordstr+"\t初始password="+password);

                        if (!oldPasswordstr.equals(password)) {
                            AbToastUtil.showToast(context, "原密码错误");
                            return;
                        }

                        myuser.setPassword(newPasswordstr);

                        myuser.update(context, new UpdateListener() {
                            @Override
                            public void onSuccess() {
                                AbToastUtil.showToast(context, "修改密码成功");
                                PreferencesUtils.putString(context, "password", newPasswordstr);
                                AbDialogUtil.removeDialog(context);
                            }

                            @Override
                            public void onFailure(int i, String s) {
                                AbToastUtil.showToast(context, "修改密码失败：" + s);
                                AbDialogUtil.removeDialog(context);

                            }
                        });


                    }
                });

            }
        });

        feedback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mLog.e("意见反馈");

                View mView = LayoutInflater.from(context).inflate(R.layout.dialog_feedback, null);
                AbDialogUtil.showDialog(mView);

                TextView title = (TextView) mView.findViewById(R.id.title);
                final EditText oldPassword = (EditText) mView.findViewById(R.id.oldPassword);
                TextView quxiao = (TextView) mView.findViewById(R.id.quxiao);
                TextView kaitong = (TextView) mView.findViewById(R.id.kaitong);

                quxiao.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AbDialogUtil.removeDialog(context);
                    }
                });
                kaitong.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mLog.e("修改用户信息：开始实现");
                        String oldPasswordstr = oldPassword.getText().toString();
                        String cid =PreferencesUtils.getString(context,"cid");
                        if (oldPasswordstr.equals("") ) {
                            AbToastUtil.showToast(context, "输入内容不能为空");
                            return;
                        }

                        Feedback feedback=new Feedback();

                        feedback.setText(oldPasswordstr);
                        feedback.setCid(cid);

                        feedback.save(context,new SaveListener() {
                            @Override
                            public void onSuccess() {
                                AbToastUtil.showToast(context, "提交意见成功");
                                AbDialogUtil.removeDialog(context);

                            }

                            @Override
                            public void onFailure(int i, String s) {
                                AbToastUtil.showToast(context, "提交意见失败：" + s);
                                AbDialogUtil.removeDialog(context);

                            }
                        });




                    }
                });

            }
        });

        checkUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AbToastUtil.showToast(context,"检查更新"+"功能正在开发中");
            }
        });

        recommend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AbToastUtil.showToast(context,"应用推荐"+"功能正在开发中");
            }
        });

        activeRelate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AbToastUtil.showToast(context,"活动相关"+"功能正在开发中");
            }
        });

        about.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AbToastUtil.showToast(context,"关于"+"功能正在开发中");
            }
        });

        clearCache.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                huancunTv.setText(0+"K");
            }
        });



    }

    private void initView() {
        zhanghao= (TextView) findViewById(R.id.zhanghao);

        newMsgNotifyBt= (LinearLayout) findViewById(R.id.newMassageBt);
        shakeNotifyBt= (LinearLayout) findViewById(R.id.zhendongBt);
        voiceNotifyBt= (LinearLayout) findViewById(R.id.voiceBt);
        exitMsgNotifyBt= (LinearLayout) findViewById(R.id.exitMassageBt);

        newMsgNotifyTv= (ImageView) findViewById(R.id.newMassageBg);
        shakeNotifyTv= (ImageView) findViewById(R.id.zhendongBg);
        voiceNotifyTv= (ImageView) findViewById(R.id.soundBg);
        exitMsgNotifyTv= (ImageView) findViewById(R.id.exitMassageBg);

        resetPassword= (LinearLayout) findViewById(R.id.resetMimaBt);
        feedback= (LinearLayout) findViewById(R.id.yijianBt);
        checkUpdate= (LinearLayout) findViewById(R.id.gengxinBt);
        recommend= (LinearLayout) findViewById(R.id.yingyongBt);
        activeRelate= (LinearLayout) findViewById(R.id.huodongBt);
        about= (LinearLayout) findViewById(R.id.guanyuBt);
        clearCache= (LinearLayout) findViewById(R.id.huancunBt);
        huancunTv= (TextView) findViewById(R.id.huancunTv);

        int temp=200+new Random().nextInt(800);
        huancunTv.setText(temp+"K");



    }

    private void initContent() {
        TextView exit= (TextView) findViewById(R.id.exit);

        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mLog.e("执行退出任务");
                BmobUserManager.getInstance(getApplicationContext()).logout();
                PreferencesUtils.clear(context);

                mLog.e("执行退出任务---完毕");


            }
        });
    }



    /***
     * 头部的监听
     */
    private void initHead() {
        //顶部
        ImageView back;//返回
        TextView title;//标题

        back= (ImageView) findViewById(R.id.back);
        title= (TextView) findViewById(R.id.title);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }






}
